package com.suxii.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.suxii.netty_demo.R;

import java.util.List;

public class NewFriendAdapter extends RecyclerView.Adapter<NewFriendAdapter.NewFriendHandler> {

    private List<String> data;
    private Context context;

    public NewFriendAdapter(List<String> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @NonNull
    @Override
    public NewFriendHandler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_newfriend, parent, false);
        return new NewFriendHandler(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NewFriendHandler holder, int position) {
        holder.name.setText(data.get(position));
        holder.agree.setOnClickListener(view -> {
            try {
                newFriendListener.onAgree(data.get(position),holder.agree);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        holder.disagree.setOnClickListener(view -> {
            try {
                newFriendListener.onDisagree(data.get(position));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public interface NewFriendListener{
        void onAgree(String uuid,TextView agree);
        void onDisagree(String uuid);
    }

    private NewFriendListener newFriendListener;

    public void OnNewFriendListener(NewFriendListener newFriendListener){
        this.newFriendListener=newFriendListener;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class NewFriendHandler extends RecyclerView.ViewHolder{

        private TextView name;
        private TextView agree;
        private TextView disagree;

        public NewFriendHandler(@NonNull View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.newfriend_name);
            agree=itemView.findViewById(R.id.newfriend_agree);
            disagree=itemView.findViewById(R.id.newfriend_disagree);
        }
    }
}
